﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace SP.SPProject.Core.Support
{
    public class ListHelper
    {

        public static bool ListExistsCurrentWeb(string listName)
        {
            return SPContext.Current.Web.Lists.TryGetList(listName) == null ? false : true;
        }

        public static bool ListContainsContentType(string contentTypeName, string listName)
        {
            if (ListExistsCurrentWeb(listName))
            {
                var existingContentType = (from contentType in SPContext.Current.Web.Lists[listName].ContentTypes.OfType<SPContentType>()
                                           where string.Compare(contentType.Name, contentTypeName, true) == 0
                                           select contentTypeName).FirstOrDefault();
                if (existingContentType == null)
                    return false;
                else
                    return true;
            }
            else
            {
                throw new Exception("List " + listName + " does not exists");
            }
        }

        public static SPListItemCollection GetItemsByCurrentUser(string listName, string fieldName)
        {

            if(!ListExistsCurrentWeb(listName))
                throw new Exception(listName+Constants.LIST_DOES_NOT_EXISTS);

            SPQuery query = new SPQuery();
            query = new SPQuery();
            query.Query = "<Where><Eq><FieldRef Name='AssignedTo' /><Value Type='Integer'><UserID Type='Integer'>1</UserID></Value></Eq></Where>";
            return SPContext.Current.Web.Lists[listName].GetItems(query);            
        }

    }
}
